package com.xcity.game.battle.res;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.xcity.db.entity.Player;
import com.xcity.game.battle.pve.template.ResStageTemplate;
import com.xcity.game.battle.pve.template.ResStageTypeTemplate;

import gnu.trove.map.hash.TIntObjectHashMap;
import naga.x.App;
import naga.x.game.template.TemplateService;

public class ResStageManager {
	
	protected Player owner;
	protected List<ResStage> stages;
	protected ResStageTypeInfo typeInfo;
	
	protected TIntObjectHashMap<List<ResStage>> type2stageList;
	public ResStageManager(Player owner, List<ResStage> stageList, ResStageTypeInfo typeInfo){
		this.owner = owner;
		this.stages = stageList != null? stageList : new ArrayList<ResStage>();
		this.typeInfo = typeInfo != null? typeInfo : new ResStageTypeInfo(ResStageTypeTemplate.SIZE);
		type2stageList = new TIntObjectHashMap<List<ResStage>>();
		for(int i = 0; i < this.stages.size(); i++){
			ResStage stage = this.stages.get(i);
			stage.setIndex(i);
			int type = stage.getTemplate().getType();
			List<ResStage> list = type2stageList.get(type);
			if(list == null){
				list = new ArrayList<ResStage>();
				type2stageList.put(type, list);
			}
			list.add(stage);
		}
	}
	
	public void initStages(List<ResStageTemplate> templates){
		List<ResStage> newer = new ArrayList<ResStage>(templates.size());
		for(int i = 0; i < stages.size(); i++){
			ResStage stage = stages.get(i);
			if(stage.getTemplate() != null){
				newer.add(stage);
			}
		}
		boolean first = newer.isEmpty();
		for(int i = 0, size = templates.size(); i < size; i++){
			ResStageTemplate template = templates.get(i);
			if(first || getStage(template.getId()) == null){
				newer.add(new ResStage(template));
			}
		}
		Collections.sort(newer);
		this.stages = newer;
		
		type2stageList = new TIntObjectHashMap<List<ResStage>>();
		for(int i = 0; i < stages.size(); i++){
			ResStage stage = this.stages.get(i);
			stage.setIndex(i);
			int type = stage.getTemplate().getType();
			List<ResStage> list = type2stageList.get(type);
			if(list == null){
				list = new ArrayList<ResStage>();
				type2stageList.put(type, list);
			}
			list.add(stage);
		}
	}
	public void initTypeInfo(List<ResStageTypeTemplate> typeTempList){
		this.typeInfo = new ResStageTypeInfo(typeTempList.size());
	}
	
	public ResStage getStage(int tempId){
		List<ResStage> list = stages;
		for(int i = 0; i < list.size(); i++){
			ResStage stage = list.get(i);
			if(stage.getId() == tempId){
				return stage;
			}
		}
		return null;
	}
	
	public ResStage getStageByIndex(int index){
		return index > -1 && index < stages.size() ?  stages.get(index) : null;
	}
	
	public int getTodayCount(ResStage stage){
		return typeInfo.type2dayFightCount.get(stage.getTemplate().getType());
	}
	
	public List<ResStage> getStagesByType(int type){
		if(type2stageList.containsKey(type)){
			return type2stageList.get(type);
		}
		return null;
	}
	
	public boolean checkType(int type){
		if(type < 0 || type >= App.getApp().getServiceManager().get(ResStageService.class).getTypeTempList().size()){
			return false;
		}
		if(type < 0 || type >= typeInfo.getType2dayFightCount().size()){
			return false;
		}
		return true;
	}
	
	public int getTodayMaxCount(ResStage stage){
		int count = App.getApp().getServiceManager().get(ResStageService.class).getTypeTempList().get(stage.getTemplate().getType()).getMaxCount();
		return count;
	}
	
	public int getLevelLimitByType(int type){
		return App.getApp().getServiceManager().get(ResStageService.class).getTypeTempList().get(type).getLevelLimit();
	}
	
	public Player getOwner() {
		return owner;
	}
	public void setOwner(Player owner) {
		this.owner = owner;
	}
	public List<ResStage> getStages() {
		return stages;
	}
	public void setStages(List<ResStage> stages) {
		this.stages = stages;
	}
	public ResStageTypeInfo getTypeInfo() {
		return typeInfo;
	}
	/**
	 * 今日已打的次数
	 */
	public int getDayCountByType(int type){
		return typeInfo.getDayCountByType(type);
	}

	@Override
	public String toString(){
		return JSON.toJSONString(stages) + ";" + JSON.toJSONString(typeInfo);
	}
}
